package com.base.system.base.db.specifications;

import javax.persistence.criteria.*;

/**
 * ^ 差
 *
 * @param <T>
 */
public class SubSpecification<T> extends AbstractSpecification<T> {
    private static final long serialVersionUID = 6690291922287079277L;
    private final String property;
    private final Object patterns;

    public SubSpecification(String property, JoinType joinType, Object v) {
        this.property = property;
        patterns = v;
        this.joinType = joinType;
    }

    @Override
    public Predicate toPredicate(Root<T> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
        From from = getFrom(property, root);
        String attr = getProperty(property);
        Expression<Integer> sub = cb.function("sub", Integer.class, cb.literal(patterns), from.get(attr));
        return cb.gt(sub, 0);
    }
}
